package com.linkgie.galaxyframework.httpproxy.converters;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.linkgie.galaxyframework.httpproxy.ResponseHandler;
import com.linkgie.galaxyframework.httpproxy.ServiceInvocationContext;
import com.linkgie.galaxyframework.httpproxy.ServiceResponse;
import com.linkgie.galaxyframework.utils.BinaryUtils;
import com.linkgie.galaxyframework.utils.RuntimeIOException;

public class ByteArrayResponseHandler implements ResponseHandler {

	public static final ByteArrayResponseHandler INSTANCE = new ByteArrayResponseHandler();

	private ByteArrayResponseHandler() {
	}

	@Override
	public Object handleResponse(com.linkgie.galaxyframework.httpproxy.ServiceRequest request, ServiceResponse response,
			ServiceInvocationContext serviceContext) throws Exception {
		try {
			InputStream responseStream = response.getInputStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			BinaryUtils.copy(responseStream, out);
			return out.toByteArray();
		} catch (IOException e) {
			throw new RuntimeIOException(e.getMessage(), e);
		}
	}

}
